# -*- coding:UTF-8 -*-
import pytesseract
from PIL import Image

def binarizing(img,thresholdMax):
	"""传入image对象进行灰度、二值处理"""
	img = img.convert("L") #转灰度
	pixdata = img.load()
	w,h = img.size
	# 遍历所有像素，大于阈值的为黑色
	for y in range(h):
		for x in range(w):
			if pixdata[x,y] < thresholdMax:
				pixdata[x, y] = 0
			else:
				pixdata[x,y] = 255
	return img

def depoint(img):
	"""传入二值化后的图片进行降噪"""
	pixdata = img.load()
	w,h = img.size
	for y in range(1,h-1):
		for x in range(1,w-1):
			count = 0
			if pixdata[x,y-1] > 245:#上
				count = count + 1
			if pixdata[x,y+1] > 245:#下
				count = count + 1
			if pixdata[x-1,y] > 245:#左
				count = count + 1
			if pixdata[x+1,y] > 245:#右
				count = count + 1
			if pixdata[x-1,y-1] > 245:#左上
				count = count + 1
			if pixdata[x-1,y+1] > 245:#左下
				count = count + 1
			if pixdata[x+1,y-1] > 245:#右上
				count = count + 1
			if pixdata[x+1,y+1] > 245:#右下
				count = count + 1
			if count > 4:
				pixdata[x,y] = 255
	return img

def pIx(img):
	data = img.load()
	# 图片的长宽
	w, h = img.size

	# data[x,y]获取目标像素点颜色。
	# data[x,y] = 255更改像素点颜色，255代表颜色。

	try:
		for x in range(1, w - 1):
			if x > 1 and x != w - 2:
				# 获取目标像素点左右位置
				left = x - 1
				right = x + 1

			for y in range(1, h - 1):
				# 获取目标像素点上下位置
				up = y - 1
				down = y + 1

				if x <= 2 or x >= (w - 2):
					data[x, y] = 255

				elif y <= 2 or y >= (h - 2):
					data[x, y] = 255

				elif data[x, y] == 0:
					if y > 1 and y != h - 1:

						# 以目标像素点为中心点，获取周围像素点颜色
						# 0为黑色，255为白色
						up_color = data[x, up]
						down_color = data[x, down]
						left_color = data[left, y]
						left_down_color = data[left, down]
						right_color = data[right, y]
						right_up_color = data[right, up]
						right_down_color = data[right, down]

						# 去掉单个黑色像素
						if up_color == 0 and down_color == 255 and right_color == 255 and left_color == 255:
							data[x,y] = 255
						elif left_color == 0 and down_color == 255 and right_color == 255 and up_color == 255:
							data[x,y] = 255
						elif down_color == 0 and up_color == 255 and right_color == 255 and left_color == 255:
							data[x,y] = 255
						elif right_color == 0 and down_color == 255 and up_color == 255 and left_color == 255:
							data[x,y] = 255

						# 去除竖线干扰线
						if down_color == 0:
							if left_color == 255 and left_down_color == 255 and \
									 data[right+1,y] == 255:
								data[x, y] = 255

						# 去除横线干扰线
						elif right_color == 0:
							if data[x,y-1] == 255 and data[x+2,y] == 0 and data[x,y-2] == 255:
								data[x, y] = 255

					# 去除斜线干扰线 and down_color == 255
					if up_color == 0 and right_color == 0 and left_color == 255 and down_color == 255\
							or (right_color == 0 and right_up_color == 0 and up_color == 255\
								and down_color == 255 and left_color ==255):
						data[x, y] = 255
				else:
					pass

		data = img.load()
		# 去掉单个黑色像素
		for x in range(1, w - 1):
			for y in range(1, h - 1):
				if data[x,y-1] == 0 and data[x,y+1] == 255 and data[x+1,y] == 255 and data[x-1,y] == 255:
					data[x, y] = 255
				elif data[x-1,y] == 0 and data[x,y+1] == 255 and data[x+1,y] == 255 and data[x,y-1] == 255:
					data[x, y] = 255
				elif data[x,y+1] == 0 and data[x,y-1] == 255 and data[x+1,y] == 255 and data[x-1,y] == 255:
					data[x, y] = 255
				elif data[x+1,y] == 0 and data[x,y+1] == 255 and data[x,y-1] == 255 and data[x-1,y] == 255:
					data[x, y] = 255

		return img
	except:
		return False

if __name__=='__main__':
	pass
	# image = Image.open('../../data/valifyCode2.jpeg')
	# image = Image.open('../../data/2312450-213374e4e7fb7241.png')
	# image = Image.open('../../data/ocr1.png')
	# image = binarizing(image,180)
	# image.save('../../report/valifyCode/11.jpeg','jpeg')
	# image = depoint(image)
	# image = depoint(image)
	# image.save('../../report/valifyCode/22.jpeg','jpeg')
	# image = pIx(image)
	# image.save("../../report/valifyCode/33.jpeg","jpeg")
	# code = pytesseract.image_to_string(image)
	# print(code)